package com.fc.hkx.api;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fc.hkx.common.ResponseJson;
import com.fc.hkx.dto.*;
import com.fc.hkx.thirdparty.airchina.*;
import com.fc.hkx.thirdparty.airchina.FlightDto;
import com.fc.hkx.thirdparty.feiyou.FeiyouApiService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * Created by charles on 2017/9/8.
 */
@RestController
@RequestMapping("/api/flight")
@Api("Air China  api")
public class AirChinaController {

    @Autowired
    private AirChinaApiService airChinaApiService;

    @Autowired
    private FeiyouApiService feiyouApiService;

    private ObjectMapper jsonMapper = new ObjectMapper();

    @RequestMapping("/list")
    public ResponseJson getAirLineList(@RequestParam Map<String, String> params) throws Exception {
        ResponseJson responseJson = new ResponseJson();
        FlightDto mainFlightDto = airChinaApiService.getAirLineList();
        List<FlightInfomation> flightList = mainFlightDto.getResp().getaGoto().getFlightInfomationList();
        for(FlightInfomation info: flightList)
        {
            String feiStr = feiyouApiService.getFlight(info.getFlightSegmentList().get(0).getFlightNo(), info.getFlightSegmentList().get(0).getFlightDepdatePlan());
            feiStr = feiStr.substring(1,feiStr.length()-1);
            com.fc.hkx.dto.FlightDto fdto = jsonMapper.readValue(feiStr, com.fc.hkx.dto.FlightDto.class);
            info.setOnTimeRate(fdto.getOntimeRate());
            info.setFlightDepAirport(fdto.getFlightArrAirport());
        }
        responseJson.setObj(mainFlightDto);
        return responseJson;
    }
}
